import {onPositionChangedCallback} from "./dlna_service";
import {onPlayerErrorCallback} from "./dlna_service";
import {onPlayerStatusChangedCallback,onVolumeChangedCallback,StatusCallback,PositionCallback,VolumeCallback} from "./dlna_service";
import DlnaService from "./dlna_service";
import rpc from "@ohos.rpc";
import common from '@ohos.app.ability.common';

export default class DlnaServiceStub extends rpc.RemoteObject implements DlnaService {
    constructor(des: string) {
        super(des);
        this.SurfaceId = ''
        this.position = 0
        this.duration = 0
        this.playerStatus = 0
        this.playFlag = true

    }
    
    async onRemoteMessageRequest(code: number, data, reply, option): Promise<boolean> {
        console.log("onRemoteMessageRequest called, code = " + code);
        data.readInterfaceToken()
        switch(code) {
            case DlnaServiceStub.COMMAND_ON_POSITION_CHANGED: {
                let _position = data.readInt();
                let _duration = data.readInt();
                this.onPositionChanged(_position, _duration, (errCode, returnValue) => {
                    reply.writeInt(errCode);
                    if (errCode == 0) {
                        reply.writeInt(returnValue);
                    }
                });
                return true;
            }
            case DlnaServiceStub.COMMAND_ON_PLAYER_STATUS_CHANGED: {
                let _playerStatus = data.readInt();
                this.onPlayerStatusChanged(_playerStatus, (errCode, returnValue) => {
                    reply.writeInt(errCode);
                    if (errCode == 0) {
                        reply.writeInt(returnValue);
                    }
                });
                return true;
            }
            case DlnaServiceStub.COMMAND_ON_PLAYER_ERROR: {
                this.onPlayerError((errCode, returnValue) => {
                    reply.writeInt(errCode);
                    if (errCode == 0) {
                        reply.writeInt(returnValue);
                    }
                });
                return true;
            }
            case DlnaServiceStub.COMMAND_ON_VOLUNE_CHANGED: {
                let _volume = data.readInt();
                let _maxVolume = data.readInt();
                this.onVolumeChanged(_volume, _maxVolume,(errCode, returnValue) => {
                    reply.writeInt(errCode);
                    if (errCode == 0) {
                        reply.writeInt(returnValue);
                    }
                });
                return true;
            }
            default: {
                console.log("invalid request code" + code);
                break;
            }
        }
        return false;
    }
    position: number
    duration: number
    playerStatus: number
    SurfaceId: string
    playFlag: boolean
    onStatusCallback: StatusCallback
    onPositionCallback:PositionCallback
    onVolumeCallback:VolumeCallback
    onPositionChanged(position: number, duration: number, callback: onPositionChangedCallback): void{
        console.log("onPositionChanged position:" + position + "duration:" + duration);
        if (this.onPositionCallback && position >= 0) {
            this.onPositionCallback(duration,position)
        }
    }
    onPlayerStatusChanged(playerStatus: number, callback: onPlayerStatusChangedCallback): void{
        if(playerStatus == 5)
        {
            this.playFlag = true
        }else
        {
            this.playFlag = false
        }
        if (this.onPositionCallback) {
            this.onStatusCallback(this.playFlag)
        }
    }
    onVolumeChanged(volume: number, maxVolume: number, callback: onVolumeChangedCallback): void{

        console.log("onVolumeChanged volume:" + volume + "maxVolume:" + maxVolume);
        if (this.onVolumeCallback) {
            this.onVolumeCallback(volume,maxVolume)
        }
    }
    onPlayerError(callback: onPlayerErrorCallback): void{
        globalThis.abilityContext.terminateSelf()
    }
    setStatusCallback(StatusCallback,PositionCallback,VolumeCallback) {
        this.onStatusCallback = StatusCallback
        this.onPositionCallback = PositionCallback
        this.onVolumeCallback = VolumeCallback
    }


    static readonly COMMAND_ON_POSITION_CHANGED = 1;
    static readonly COMMAND_ON_PLAYER_STATUS_CHANGED = 2;
    static readonly COMMAND_ON_PLAYER_ERROR = 3;
    static readonly COMMAND_ON_VOLUNE_CHANGED = 4;
}

